//
//  subView3ForGetJobSheet.m
//  HondaPro_2
//
//  Created by Eric on 13-6-20.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "subView3ForGetJobSheet.h"
#import "UserDefinedCellForSubView3ForGetJobSheet.h"

@interface subView3ForGetJobSheet ()

/** 收费区分列表 */
@property(nonatomic,strong)NSMutableDictionary *chargeDiff;

//创建控件
-(void)createStaticLabels;
-(void)createTableView;

//刷新数据
-(void)freshenViewDis;

@end

@implementation subView3ForGetJobSheet

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        [self createStaticLabels];
        [self createTableView];
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    
    [self freshenViewDis];
}

#pragma mark - 创建控件

-(void)createStaticLabels
{
    // @"DISCOUNT"
    NSArray *arrForAllKeys = [NSArray arrayWithObjects:
                              @"CHARGE_PARTITION_CODE",
                              @"ADD_ITEM_CODE",
                              @"ADD_ITEM_NAME",
                              @"ADD_ITEM_AMOUNT",nil];

    CGFloat lblY = 0;
    CGFloat lblH = 50;
    CGFloat lblW = self.frame.size.width / arrForAllKeys.count;
    //@"折扣",@"DISCOUNT"
    NSDictionary *dicForStaticLabelsText = [NSDictionary dictionaryWithObjectsAndKeys:
                                            @"收费区分",@"CHARGE_PARTITION_CODE",
                                            @"附加项目代码",@"ADD_ITEM_CODE",
                                            @"附加项目名称",@"ADD_ITEM_NAME",
                                            @"金额",@"ADD_ITEM_AMOUNT",nil];
    
    for (int i = 0; i<[arrForAllKeys count]; i++)
    {
        NSString *strKeys = [arrForAllKeys objectAtIndex:i];
        CGRect rect = CGRectMake(i * lblW, lblY, lblW, lblH);
        UILabel *lbl = [[UILabel alloc] init];
        lbl.frame = rect;
        lbl.textColor = [UIColor grayColor];
        lbl.backgroundColor = [UIColor groupTableViewBackgroundColor];
        lbl.font = [UIFont fontWithName:@"ArialMT" size:16];
        lbl.text = [dicForStaticLabelsText objectForKey:strKeys];
        lbl.textAlignment=NSTextAlignmentCenter;
        [self addSubview:lbl];
    }
}

-(void)createTableView
{
    tableViewDis = [[UITableView alloc] initWithFrame:CGRectMake(0, 30+21, self.frame.size.width, 168-21)];
    tableViewDis.backgroundColor = [UIColor clearColor];
    
    tableViewDis.dataSource = self;
    tableViewDis.delegate = self;
    
    [self addSubview:tableViewDis];
}

#pragma mark - 刷新数据

-(void)freshenViewDis
{
    [tableViewDis reloadData];
}

#pragma mark - tableView dataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    return 3+modelIns.attachProjects.count;
}

//哪几行可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.canEdit) {
        MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
        
        if (indexPath.row<modelIns.attachProjects.count)
        {
            return YES;
        }
    }
    return NO;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UserDefinedCellForSubView3ForGetJobSheet *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (nil == cell)
    {
        cell = [[UserDefinedCellForSubView3ForGetJobSheet alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
        
    //cell的风格是：浅浅灰色和浅灰色间隔出现，第一行是浅灰色
    NSInteger row = [indexPath row];
    switch (row%2)
    {
        case 0:
            //浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
            break;
        case 1:
            //浅浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
            break;
            
        default:
            break;
    }
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];

    NSArray *arrRepairItemList = modelIns.attachProjects;
    if (row<[arrRepairItemList count])
    {
        NSDictionary *dicRepairItemList = [arrRepairItemList objectAtIndex:row];
        for (int i = 0; i<[cell.arrForAllLabelsKeysOnCell count]; i++)
        {
            NSString *strKey =cell.arrForAllLabelsKeysOnCell[i];
            id text = dicRepairItemList[strKey];
            if ([text isKindOfClass:[NSNumber class]]) {//如果是数字类型
                text = [text stringValue];
            }
            else if ([strKey isEqualToString:@"CHARGE_PARTITION_CODE"]){
                text = self.chargeDiff[dicRepairItemList[strKey]];
            }

            ((UILabel *)cell.dicForAllLabelsOnCell[strKey]).text = text;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    else
    {
        for (int i = 0; i<[cell.arrForAllLabelsKeysOnCell count]; i++)
        {
            ((UILabel *)cell.dicForAllLabelsOnCell[cell.arrForAllLabelsKeysOnCell[i]]).text = @"";
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return cell;
}

#pragma mark - tableView delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    
    //在此 将业务模型中对应的属性 设定为 当前选中的为第几行
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    if (row<modelIns.attachProjects.count)
    {
        modelIns.nRowForOtherChargeList = row;
    }
    else
    {
        modelIns.nRowForOtherChargeList = -1;
    }
}

#pragma mark - 出现删除按钮时，点击按钮时的操作

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    NSMutableDictionary *dic = modelIns.attachProjects[indexPath.row];
    if (!dic[@"STATUS"]||[dic[@"STATUS"] isEqualToString:@"U"]) {//如果是存在数据库中的数据
        [dic setObject:@"D" forKey:@"STATUS"];
        [modelIns.deletedAttachProjects addObject:dic];
    }
    [modelIns.attachProjects removeObjectAtIndex:indexPath.row];        
    [self freshenViewDis];
    [[NSNotificationCenter defaultCenter]postNotificationName:OTHER_CHARGE_LIST_MSG object:nil];
}

-(NSMutableDictionary *)chargeDiff{
    if (_chargeDiff == nil) {
        _chargeDiff = [NSMutableDictionary dictionary];
        NSMutableArray *arr = [self readershuDICimform][@"TM_CHARGE_PARTITION"];
        for (NSDictionary *dic in arr) {
            NSString *name = dic[@"NAME"];
            NSString *val = dic[@"CODE"];
            [_chargeDiff setObject:name forKey:val];
        }
    }
    return _chargeDiff;
}

-(NSMutableDictionary *)readershuDICimform
{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path=[paths objectAtIndex:0];
    NSString *str=[path stringByAppendingPathComponent:@"Dic.text"];
    dic=[NSMutableDictionary dictionaryWithContentsOfFile:str];
    //nslog(@"%@",dic);
    return dic;
}

@end
